package cn.gyouzhe.component.runner;

import cn.gyouzhe.entity.Coupon;
import cn.gyouzhe.service.ICouponService;
import cn.gyouzhe.service.IGoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 过期券验证执行器
 *
 * @author wangchuan
 * @datetime 2018/4/17 21:28
 */
@Slf4j
@Component
public class ExpireCouponCheckRunner {
    @Autowired
    private IGoodService goodService;
    @Autowired
    private ICouponService couponService;

    /**
     * 定时验证
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void check() {
        try {
            List<Coupon> coupons = couponService.selectExpireCoupon(LocalDateTime.now());
            for(Coupon coupon : coupons) {
                goodService.expireGood(coupon.getGoodId());
                couponService.deleteCoupon(coupon.getId());
            }
        } catch(Exception e) {
            log.error("处理过期券信息异常", e);
        }
    }
}
